import 'package:flutter/material.dart';
import 'package:flutter_demo2/leak/leak_watcher/src/leak/model/leak_node.dart';

class LeakWidget extends StatefulWidget {
  final List<LeakNode> list;

  const LeakWidget(this.list, {Key? key}) : super(key: key);

  @override
  State<LeakWidget> createState() => LeakWidgetState();
}

class LeakWidgetState extends State<LeakWidget> {
  final ScrollController _scrollController = ScrollController();

  void scroll2top() {
    _scrollController.animateTo(0, duration: const Duration(milliseconds: 200), curve: Curves.ease);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
        color: Color(0xff1e212c),
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(14.0),
          topRight: Radius.circular(14.0),
        ),
      ),
      child: Column(
        children: [
          Container(
            color: const Color(0xff4a2c2c),
            height: 45,
            alignment: Alignment.center,
            child: Text(widget.list.isEmpty ? "" : widget.list[0].name ?? "", style: _title1Style),
          ),
          Expanded(
            child: ListView.builder(
              controller: _scrollController,
              itemBuilder: (_, index) {
                final node = widget.list[index];
                bool isField = node.nodeType == NodeType.FIELD.toString();
                return isField ? _buildField(node) : _buildClass(node);
              },
              itemCount: widget.list.length,
            ),
          )
        ],
      ),
    );
  }

  Widget _buildField(LeakNode node) {
    return Container(
      margin: const EdgeInsets.all(8),
      padding: const EdgeInsets.all(8),
      decoration: const BoxDecoration(
          color: Color(0xff32aaff), borderRadius: BorderRadius.all(Radius.circular(8.0))),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            padding: const EdgeInsets.only(top: 12),
            child: Text(
              'gc root field - > ${node.name!}',
              style: _title2Style,
            ),
          ),
          Container(
              padding: const EdgeInsets.only(top: 12),
              child: Text(
                node.codeInfo?.toString() ?? '',
                style: _title3Style,
              )),
          Container(
            padding: const EdgeInsets.only(top: 12),
            child: Text(
              'uri - > ${node.codeInfo?.uri}',
              style: _title3Style,
            ),
          )
        ],
      ),
    );
  }

  Widget _buildClass(LeakNode node) {
    return Container(
      margin: const EdgeInsets.all(8),
      padding: const EdgeInsets.all(16),
      decoration: const BoxDecoration(
          color: Color(0xff0a4c49), borderRadius: BorderRadius.all(Radius.circular(8.0))),
      child: Center(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              node.name!,
              style: _title2Style,
            ),
            Text(
              node.getParent(),
              style: _title3Style,
            )
          ],
        ),
      ),
    );
  }
}

final _title1Style = TextStyle(fontSize: 18, color: Colors.white.withOpacity(0.9));
final _title2Style = TextStyle(fontSize: 15, color: Colors.white.withOpacity(0.75));
final _title3Style = TextStyle(fontSize: 12, color: Colors.white.withOpacity(0.5));
